R, RStudio ve RMarkdown ile Tekrarlanabilir Rapor1

Serdar Balcı, MD, Pathologist

2019-09-29

Tekrarlanabilir Analiz ve Rapor

Replication Crisis

https://en.wikipedia.org/wiki/Replication_crisis

Replication Crisis

RStudio ile proje oluştur

R Notebook

R Notebook

R Notebook

RNotebook vs RMarkdown

https://youtu.be/zNzZ1PfUDNk

R Markdown

R Markdown: The Definitive Guide

https://bookdown.org/yihui/rmarkdown/

Hem kendi kodları hem de html kodları yazılabilir.

R Markdown syntax

https://gist.github.com/MinhasKamal/7fdebb7c424d23149140

Remedy

Remedy

Use R Markdown

https://andrewbtran.github.io/NICAR/2018/workflow/docs/02-rmarkdown.html

  • Troubleshooting in R Markdown

https://smithcollege-sds.github.io/sds-public/rmarkdown_problems.html

Use R Markdown

http://kbroman.org/knitr_knutshell/pages/Rmarkdown.html

  • Knitr & R Markdown

https://kbroman.org/knitr_knutshell/pages/overview.html

https://kbroman.org/knitr_knutshell/pages/Rmarkdown.html

https://kbroman.org/knitr_knutshell/pages/markdown.html

Use R Markdown

  • render markdown via code

inside R

markdown::markdownToHTML('markdown_example.md', 
'markdown_example.html')

command line

R -e "markdown::markdownToHTML('markdown_example.md',
'markdown_example.html')"

Use R Markdown

  • pandoc Rstudio integration

command line

export PATH=$PATH:/Applications/RStudio.app/Contents/MacOS/pandoc
R -e "rmarkdown::render('markdown_example.md')"

Use R Markdown

{r, results='asis'}
 iris %>%
  tibble::as_tibble() %>%
  details::details(summary = 'tibble')

Use R Markdown

  • Keeping Credentials Secret with Keyrings in R

https://ras44.github.io/blog/2019/01/19/keeping-credentials-secret-with-keyrings-in-r.html

  • How to build a website with Blogdown in R

http://www.storybench.org/how-to-build-a-website-with-blogdown-in-r/

Chunk Options

{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width = 12, fig.height = 8, fig.path = 'Figs/', echo = FALSE, warning = FALSE, message = FALSE, error = FALSE, eval = TRUE, tidy = TRUE, comment = NA)

R Markdown örneği

R Markdown örneği

    inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
1      3  306      2  74   1       1       90       100     1175      NA
2      3  455      2  68   1       0       90        90     1225      15
3      3 1010      1  56   1       0       90        90       NA      15
4      5  210      2  57   1       1       90        60     1150      11
5      1  883      2  60   1       0      100        90       NA       0
6     12 1022      1  74   1       1       50        80      513       0
7      7  310      2  68   2       2       70        60      384      10
8     11  361      2  71   2       2       60        80      538       1
9      1  218      2  53   1       1       70        80      825      16
10     7  166      2  61   1       2       70        70      271      34
11     6  170      2  57   1       1       80        80     1025      27
12    16  654      2  68   2       2       70        70       NA      23
13    11  728      2  68   2       1       90        90       NA       5
14    21   71      2  60   1      NA       60        70     1225      32
15    12  567      2  57   1       1       80        70     2600      60
16     1  144      2  67   1       1       80        90       NA      15
17    22  613      2  70   1       1       90       100     1150      -5
18    16  707      2  63   1       2       50        70     1025      22
19     1   61      2  56   2       2       60        60      238      10
20    21   88      2  57   1       1       90        80     1175      NA
21    11  301      2  67   1       1       80        80     1025      17
22     6   81      2  49   2       0      100        70     1175      -8
23    11  624      2  50   1       1       70        80       NA      16
24    15  371      2  58   1       0       90       100      975      13
25    12  394      2  72   1       0       90        80       NA       0
26    12  520      2  70   2       1       90        80      825       6
27     4  574      2  60   1       0      100       100     1025     -13
28    13  118      2  70   1       3       60        70     1075      20
29    13  390      2  53   1       1       80        70      875      -7
30     1   12      2  74   1       2       70        50      305      20
31    12  473      2  69   2       1       90        90     1025      -1
32     1   26      2  73   1       2       60        70      388      20
33     7  533      2  48   1       2       60        80       NA     -11
34    16  107      2  60   2       2       50        60      925     -15
35    12   53      2  61   1       2       70       100     1075      10
36     1  122      2  62   2       2       50        50     1025      NA
37    22  814      2  65   1       2       70        60      513      28
38    15  965      1  66   2       1       70        90      875       4
39     1   93      2  74   1       2       50        40     1225      24
40     1  731      2  64   2       1       80       100     1175      15
41     5  460      2  70   1       1       80        60      975      10
42    11  153      2  73   2       2       60        70     1075      11
43    10  433      2  59   2       0       90        90      363      27
44    12  145      2  60   2       2       70        60       NA      NA
45     7  583      2  68   1       1       60        70     1025       7
46     7   95      2  76   2       2       60        60      625     -24
47     1  303      2  74   1       0       90        70      463      30
48     3  519      2  63   1       1       80        70     1025      10
49    13  643      2  74   1       0       90        90     1425       2
50    22  765      2  50   2       1       90       100     1175       4
51     3  735      2  72   2       1       90        90       NA       9
52    12  189      2  63   1       0       80        70       NA       0
53    21   53      2  68   1       0       90       100     1025       0
54     1  246      2  58   1       0      100        90     1175       7
55     6  689      2  59   1       1       90        80     1300      15
56     1   65      2  62   1       0       90        80      725      NA
57     5    5      2  65   2       0      100        80      338       5
58    22  132      2  57   1       2       70        60       NA      18
59     3  687      2  58   2       1       80        80     1225      10
60     1  345      2  64   2       1       90        80     1075      -3
61    22  444      2  75   2       2       70        70      438       8
62    12  223      2  48   1       1       90        80     1300      68
63    21  175      2  73   1       1       80       100     1025      NA
64    11   60      2  65   2       1       90        80     1025       0
65     3  163      2  69   1       1       80        60     1125       0
66     3   65      2  68   1       2       70        50      825       8
67    16  208      2  67   2       2       70        NA      538       2
68     5  821      1  64   2       0       90        70     1025       3
69    22  428      2  68   1       0      100        80     1039       0
70     6  230      2  67   1       1       80       100      488      23
71    13  840      1  63   1       0       90        90     1175      -1
72     3  305      2  48   2       1       80        90      538      29
73     5   11      2  74   1       2       70       100     1175       0
74     2  132      2  40   1       1       80        80       NA       3
75    21  226      2  53   2       1       90        80      825       3
76    12  426      2  71   2       1       90        90     1075      19
77     1  705      2  51   2       0      100        80     1300       0
78     6  363      2  56   2       1       80        70     1225      -2
79     3   11      2  81   1       0       90        NA      731      15
80     1  176      2  73   1       0       90        70      169      30
81     4  791      2  59   1       0      100        80      768       5
82    13   95      2  55   1       1       70        90     1500      15
83    11  196      1  42   1       1       80        80     1425       8
84    21  167      2  44   2       1       80        90      588      -1
85    16  806      1  44   1       1       80        80     1025       1
86     6  284      2  71   1       1       80        90     1100      14
87    22  641      2  62   2       1       80        80     1150       1
88    21  147      2  61   1       0      100        90     1175       4
89    13  740      1  44   2       1       90        80      588      39
90     1  163      2  72   1       2       70        70      910       2
91    11  655      2  63   1       0      100        90      975      -1
92    22  239      2  70   1       1       80       100       NA      23
93     5   88      2  66   1       1       90        80      875       8
94    10  245      2  57   2       1       80        60      280      14
95     1  588      1  69   2       0      100        90       NA      13
96    12   30      2  72   1       2       80        60      288       7
97     3  179      2  69   1       1       80        80       NA      25
98    12  310      2  71   1       1       90       100       NA       0
99    11  477      2  64   1       1       90       100      910       0
100    3  166      2  70   2       0       90        70       NA      10
 [ reached 'max' / getOption("max.print") -- omitted 128 rows ]
    inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
1      3  306      2  74   1       1       90       100     1175      NA
2      3  455      2  68   1       0       90        90     1225      15
3      3 1010      1  56   1       0       90        90       NA      15
4      5  210      2  57   1       1       90        60     1150      11
5      1  883      2  60   1       0      100        90       NA       0
6     12 1022      1  74   1       1       50        80      513       0
7      7  310      2  68   2       2       70        60      384      10
8     11  361      2  71   2       2       60        80      538       1
9      1  218      2  53   1       1       70        80      825      16
10     7  166      2  61   1       2       70        70      271      34
11     6  170      2  57   1       1       80        80     1025      27
12    16  654      2  68   2       2       70        70       NA      23
13    11  728      2  68   2       1       90        90       NA       5
14    21   71      2  60   1      NA       60        70     1225      32
15    12  567      2  57   1       1       80        70     2600      60
16     1  144      2  67   1       1       80        90       NA      15
17    22  613      2  70   1       1       90       100     1150      -5
18    16  707      2  63   1       2       50        70     1025      22
19     1   61      2  56   2       2       60        60      238      10
20    21   88      2  57   1       1       90        80     1175      NA
21    11  301      2  67   1       1       80        80     1025      17
22     6   81      2  49   2       0      100        70     1175      -8
23    11  624      2  50   1       1       70        80       NA      16
24    15  371      2  58   1       0       90       100      975      13
25    12  394      2  72   1       0       90        80       NA       0
26    12  520      2  70   2       1       90        80      825       6
27     4  574      2  60   1       0      100       100     1025     -13
28    13  118      2  70   1       3       60        70     1075      20
29    13  390      2  53   1       1       80        70      875      -7
30     1   12      2  74   1       2       70        50      305      20
31    12  473      2  69   2       1       90        90     1025      -1
32     1   26      2  73   1       2       60        70      388      20
33     7  533      2  48   1       2       60        80       NA     -11
34    16  107      2  60   2       2       50        60      925     -15
35    12   53      2  61   1       2       70       100     1075      10
36     1  122      2  62   2       2       50        50     1025      NA
37    22  814      2  65   1       2       70        60      513      28
38    15  965      1  66   2       1       70        90      875       4
39     1   93      2  74   1       2       50        40     1225      24
40     1  731      2  64   2       1       80       100     1175      15
41     5  460      2  70   1       1       80        60      975      10
42    11  153      2  73   2       2       60        70     1075      11
43    10  433      2  59   2       0       90        90      363      27
44    12  145      2  60   2       2       70        60       NA      NA
45     7  583      2  68   1       1       60        70     1025       7
46     7   95      2  76   2       2       60        60      625     -24
47     1  303      2  74   1       0       90        70      463      30
48     3  519      2  63   1       1       80        70     1025      10
49    13  643      2  74   1       0       90        90     1425       2
50    22  765      2  50   2       1       90       100     1175       4
51     3  735      2  72   2       1       90        90       NA       9
52    12  189      2  63   1       0       80        70       NA       0
53    21   53      2  68   1       0       90       100     1025       0
54     1  246      2  58   1       0      100        90     1175       7
55     6  689      2  59   1       1       90        80     1300      15
56     1   65      2  62   1       0       90        80      725      NA
57     5    5      2  65   2       0      100        80      338       5
58    22  132      2  57   1       2       70        60       NA      18
59     3  687      2  58   2       1       80        80     1225      10
60     1  345      2  64   2       1       90        80     1075      -3
61    22  444      2  75   2       2       70        70      438       8
62    12  223      2  48   1       1       90        80     1300      68
63    21  175      2  73   1       1       80       100     1025      NA
64    11   60      2  65   2       1       90        80     1025       0
65     3  163      2  69   1       1       80        60     1125       0
66     3   65      2  68   1       2       70        50      825       8
67    16  208      2  67   2       2       70        NA      538       2
68     5  821      1  64   2       0       90        70     1025       3
69    22  428      2  68   1       0      100        80     1039       0
70     6  230      2  67   1       1       80       100      488      23
71    13  840      1  63   1       0       90        90     1175      -1
72     3  305      2  48   2       1       80        90      538      29
73     5   11      2  74   1       2       70       100     1175       0
74     2  132      2  40   1       1       80        80       NA       3
75    21  226      2  53   2       1       90        80      825       3
76    12  426      2  71   2       1       90        90     1075      19
77     1  705      2  51   2       0      100        80     1300       0
78     6  363      2  56   2       1       80        70     1225      -2
79     3   11      2  81   1       0       90        NA      731      15
80     1  176      2  73   1       0       90        70      169      30
81     4  791      2  59   1       0      100        80      768       5
82    13   95      2  55   1       1       70        90     1500      15
83    11  196      1  42   1       1       80        80     1425       8
84    21  167      2  44   2       1       80        90      588      -1
85    16  806      1  44   1       1       80        80     1025       1
86     6  284      2  71   1       1       80        90     1100      14
87    22  641      2  62   2       1       80        80     1150       1
88    21  147      2  61   1       0      100        90     1175       4
89    13  740      1  44   2       1       90        80      588      39
90     1  163      2  72   1       2       70        70      910       2
91    11  655      2  63   1       0      100        90      975      -1
92    22  239      2  70   1       1       80       100       NA      23
93     5   88      2  66   1       1       90        80      875       8
94    10  245      2  57   2       1       80        60      280      14
95     1  588      1  69   2       0      100        90       NA      13
96    12   30      2  72   1       2       80        60      288       7
97     3  179      2  69   1       1       80        80       NA      25
98    12  310      2  71   1       1       90       100       NA       0
99    11  477      2  64   1       1       90       100      910       0
100    3  166      2  70   2       0       90        70       NA      10
 [ reached 'max' / getOption("max.print") -- omitted 128 rows ]

R Markdown örneği

$properties
      property       value
1        class  data.frame
2       typeof        list
3         mode        list
4 storage.mode        list
5          dim    228 x 10
6       length          10
7    is.object        TRUE
8  object.type          S3
9  object.size 34752 Bytes

$attributes.lengths
    names     class row.names 
       10         1       228 

$extensive.is
                  test value
7        is.data.frame  TRUE
20             is.list  TRUE
33           is.object  TRUE
42        is.recursive  TRUE
1             is.array FALSE
2            is.atomic FALSE
3              is.call FALSE
4         is.character FALSE
5           is.complex FALSE
6             is.Coord FALSE
8            is.double FALSE
9       is.environment FALSE
10       is.expression FALSE
11            is.facet FALSE
12           is.factor FALSE
13         is.function FALSE
14           is.ggplot FALSE
15          is.ggproto FALSE
16       is.grouped_df FALSE
17          is.integer FALSE
18         is.language FALSE
19             is.leaf FALSE
21          is.logical FALSE
22           is.matrix FALSE
23              is.mts FALSE
26             is.name FALSE
27             is.null FALSE
28          is.numeric FALSE
29  is.numeric_version FALSE
30     is.numeric.Date FALSE
31 is.numeric.difftime FALSE
32   is.numeric.POSIXt FALSE
34          is.ordered FALSE
35  is.package_version FALSE
36         is.pairlist FALSE
37        is.primitive FALSE
38               is.qr FALSE
39           is.raster FALSE
40        is.ratetable FALSE
41              is.raw FALSE
43       is.relistable FALSE
44              is.src FALSE
45          is.stepfun FALSE
46             is.Surv FALSE
48           is.symbol FALSE
49            is.table FALSE
50              is.tbl FALSE
51            is.theme FALSE
52           is.tibble FALSE
53               is.ts FALSE
54         is.tskernel FALSE
56           is.vector FALSE
24               is.na FALSE
25    is.na.data.frame FALSE
47         is.survival FALSE
55         is.unsorted  <NA>
                                                                         warnings
7                                                                                
20                                                                               
33                                                                               
42                                                                               
1                                                                                
2                                                                                
3                                                                                
4                                                                                
5                                                                                
6                                                                                
8                                                                                
9                                                                                
10                                                                               
11                                                                               
12                                                                               
13                                                                               
14                                                                               
15                                                                               
16                                                                               
17                                                                               
18                                                                               
19                                                                               
21                                                                               
22                                                                               
23                                                                               
26                                                                               
27                                                                               
28                                                                               
29                                                                               
30                                                                               
31                                                                               
32                                                                               
34                                                                               
35                                                                               
36                                                                               
37                                                                               
38                                                                               
39                                                                               
40                                                                               
41                                                                               
43                                                                               
44                                                                               
45                                                                               
46                                                                               
48                                                                               
49                                                                               
50                                                                               
51                                                                               
52                                                                               
53                                                                               
54                                                                               
56                                                                               
24 !!! Logical value applies only to the first element of the provided object !!!
25 !!! Logical value applies only to the first element of the provided object !!!
47 !!! Logical value applies only to the first element of the provided object !!!
55                                                                               

R Markdown örneği

summarytools descriptives

R Markdown örneği

recode

R Markdown örneği

crosstables

R Markdown örneği

survival

Güncellemeler olunca kodlar çalışacak mı?

Paket Kütüphaneleri

  • packrat

  • renv

https://environments.rstudio.com

Docker

  • docker

Yeni R sürümleri

  • RSwitch

https://rud.is/rswitch/

  • Using RSwitch

https://rud.is/rswitch/guide/

: scale 30%

Yedeklemeyi nasıl yapacağız

Save Final Data

{r}
saved data after analysis to `mydata.xlsx`.

save.image(file = here::here("data", "mydata_work_space.RData"))

readr::write_rds(x = mydata, path = here::here("data", "mydata_afteranalysis.rds"))

saveRDS(object = mydata, file = here::here("data", "mydata.rds"))

writexl::write_xlsx(mydata, here::here("data", "mydata.xlsx"))

paste0(rownames(file.info(here::here("data", "mydata.xlsx"))), " : ", file.info(here::here("data", "mydata.xlsx"))$ctime)

GitHub

{r github push}
CommitMessage <- paste("updated on ", Sys.time(), sep = "")
wd <- getwd()
gitCommand <- paste("cd ", 
                    wd,
                    " \n git add . \n git commit --message '",
                    CommitMessage,
                    "' \n git push origin master \n",
                    sep = ""
                    )
system(command = gitCommand,
       intern = TRUE
)

GitHub Yedekleme

CommitMessage <- paste("updated on ", Sys.time(), sep = "")
wd <- getwd()
gitCommand <- paste("cd ", wd, " \n git add . \n git commit --message '", CommitMessage, 
    "' \n git push origin master \n", sep = "")

system(command = gitCommand, intern = TRUE)
[1] "[master 4ad79e2] updated on 2019-09-29 11:32:43"    
[2] " 3 files changed, 48 insertions(+), 12 deletions(-)"

Her dökümanın sonuna kullandığınız kütüphaneler için atıf yazdırabilirsiniz

{r library citation, echo=TRUE}
citation()

Libraries Used

citation()

To cite R in publications use:

  R Core Team (2019). R: A language and environment for
  statistical computing. R Foundation for Statistical Computing,
  Vienna, Austria. URL https://www.R-project.org/.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {R: A Language and Environment for Statistical Computing},
    author = {{R Core Team}},
    organization = {R Foundation for Statistical Computing},
    address = {Vienna, Austria},
    year = {2019},
    url = {https://www.R-project.org/},
  }

We have invested a lot of time and effort in creating R, please
cite it when using it for data analysis. See also
'citation("pkgname")' for citing R packages.

Her dökümanın sonuna oturum detaylarınızı yazdırabilirsiniz

{r session info, echo=TRUE}
sessionInfo()

Session Info

sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] finalfit_0.9.5    survival_2.44-1.1 forcats_0.4.0    
 [4] stringr_1.4.0     dplyr_0.8.3       purrr_0.3.2      
 [7] readr_1.3.1       tidyr_1.0.0       tibble_2.1.3     
[10] ggplot2_3.2.1     tidyverse_1.2.1  

loaded via a namespace (and not attached):
 [1] nlme_3.1-141        bitops_1.0-6        matrixStats_0.55.0 
 [4] lubridate_1.7.4     RColorBrewer_1.1-2  httr_1.4.1         
 [7] tools_3.6.0         backports_1.1.4     R6_2.4.0           
[10] rpart_4.1-15        Hmisc_4.2-0         lazyeval_0.2.2     
[13] colorspace_1.4-1    jomo_2.6-9          nnet_7.3-12        
[16] withr_2.1.2         tidyselect_0.2.5    gridExtra_2.3      
[19] compiler_3.6.0      cli_1.1.0           rvest_0.3.4        
[22] formatR_1.7         htmlTable_1.13.2    mice_3.6.0         
[25] xml2_1.2.2          scales_1.0.0        checkmate_1.9.4    
[28] digest_0.6.21       minqa_1.2.4         foreign_0.8-72     
[31] rmarkdown_1.15      base64enc_0.1-3     pkgconfig_2.0.3    
[34] htmltools_0.3.6     lme4_1.1-21         htmlwidgets_1.3    
[37] rlang_0.4.0         readxl_1.3.1        rstudioapi_0.10    
[40] xaringan_0.12       pryr_0.1.4          generics_0.0.2     
[43] jsonlite_1.6        acepack_1.4.1       RCurl_1.95-4.12    
[46] magrittr_1.5        rapportools_1.0     Formula_1.2-3      
[49] Matrix_1.2-17       Rcpp_1.0.2          munsell_0.5.0      
[52] lifecycle_0.1.0     stringi_1.4.3       yaml_2.2.0         
[55] MASS_7.3-51.4       plyr_1.8.4          grid_3.6.0         
[58] promises_1.0.1      parallel_3.6.0      crayon_1.3.4       
[61] mitml_0.3-7         lattice_0.20-38     haven_2.1.1        
[64] splines_3.6.0       pander_0.6.3        summarytools_0.9.4 
[67] hms_0.5.1           magick_2.2          zeallot_0.1.0      
[70] knitr_1.25          pillar_1.4.2        tcltk_3.6.0        
[73] boot_1.3-23         revealjs_0.9        codetools_0.2-16   
[76] pan_1.6             servr_0.15          glue_1.3.1         
[79] evaluate_0.14       latticeExtra_0.6-28 data.table_1.12.2  
[82] renv_0.6.0-141      modelr_0.1.5        httpuv_1.5.2       
[85] nloptr_1.2.1        vctrs_0.2.0         cellranger_1.1.0   
[88] gtable_0.3.0        assertthat_0.2.1    xfun_0.9           
[91] mime_0.7            broom_0.5.2         later_0.8.0        
[94] cluster_2.1.0      

Sonraki Konular

  • RStudio ile GitHub kullanımı

Sunum Linkleri

https://sbalci.github.io/MyRCodesForDataAnalysis/R-Markdown.nb.html https://sbalci.github.io/MyRCodesForDataAnalysis/R-Markdown.html

https://forms.gle/UqGJBiAjB8uLPRon8

Geri Bildirim

İletişim

Completed on 2019-09-29.

Serdar Balci, MD, Pathologist

https://rpubs.com/sbalci/CV
https://sbalci.github.io/
https://github.com/sbalci
https://twitter.com/serdarbalci


  1. Bu bir derlemedir, mümkün mertebe alıntılara linklerle referans vermeye çalıştım.↩︎